package com.stormragetech.teamwork.module.user.spring.dao.impl;

import com.stormragetech.common.dao.MysqlBaseDao;
import com.stormragetech.common.exception.StormragetechException;
import com.stormragetech.teamwork.common.constant.Constants;
import com.stormragetech.teamwork.module.user.entity.Authority;
import com.stormragetech.teamwork.module.user.spring.dao.AuthorityDao;
import lombok.extern.log4j.Log4j2;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * description: //TODO
 *
 * @author: Xue Bin
 * @version: 1.0.0-SNAPSHOT
 */
@Log4j2
@Repository
public class AuthorityDaoImpl extends MysqlBaseDao implements AuthorityDao {

    public AuthorityDaoImpl(NamedParameterJdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    public int create(final Authority entity) throws StormragetechException {

        final String query = "INSERT INTO authority " +
                "(name, type, sign, locked) " +
                "VALUES " +
                "(:name, :type, :sign, :locked)";

        try {
            return create(query, entity);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }

    @Override
    public Authority queryById(final Map<String, Object> map) throws StormragetechException {

        final String query = "SELECT * " +
                "FROM authority " +
                "WHERE id = :id";

        try {
            return queryObject(query, Authority.class, map);

        } catch (Exception e) {
            throw new StormragetechException(Constants.DAO_ERR_CODE, e.getMessage(), e);
        }
    }
}
